﻿using GodSystemBLL;
using GodSystemCommon;
using GodSystemModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormUserList : Form
    {
        public FormUserList()
        {
            InitializeComponent();
            GetUserList();
            InitPower();
        }

        /// <summary>
        /// 设置权限可见性
        /// </summary>
        private void InitPower()
        {
            // 设置坐标
            int x = 414;
            int y = 44;

            // 按钮间隔
            int sep = 36;

            // 设置大小
            int sizeWidth = 90;
            int sizeHeight = 30;
            // 添加用户功能
            if (!Utility.CheckUserPower("AddUser"))
            {
                button_UserAdd.Visible = false;
            }
            else
            {
                button_UserAdd.Location = new Point(x,y);
                button_UserAdd.Size = new Size(sizeWidth, sizeHeight);
                x += sep + sizeWidth;
            }

            // 修改用户
            if (!Utility.CheckUserPower("UpdateUser"))
            {
                button_UserModify.Visible = false;
            }
            else
            {
                button_UserModify.Location = new Point(x, y);
                button_UserModify.Size = new Size(sizeWidth, sizeHeight);
                x += sep + sizeWidth;
            }

            // 修改权限
            if (!Utility.CheckUserPower("UpdateUserPower"))
            {
                button_UpdatePower.Visible = false;
            }
            else
            {
                button_UpdatePower.Location = new Point(x, y);
                button_UpdatePower.Size = new Size(sizeWidth, sizeHeight);
                x += sep + sizeWidth;
            }

            // 修改角色
            if (!Utility.CheckUserPower("UpdateUserRole"))
            {
                button_RoleUpdate.Visible = false;
            }
            else
            {
                button_RoleUpdate.Location = new Point(x, y);
                button_RoleUpdate.Size = new Size(sizeWidth, sizeHeight);
            }



        }
        public static UsersModel usersModel;
        private void GetUserList()
        {
            int perPage = GlobalData.PerPage;
            int totalPage = 0;
            int currentrPage = GlobalData.CurrentPage;
            if (currentrPage == 1)
            {
                button_index.Enabled = false;
                button_preview.Enabled = false;
            }
            else
            {
                button_index.Enabled = true;
                button_preview.Enabled = true;
            }

            string userName = textBox_search.Text.Trim();
            UsersBLL usersBLL = new UsersBLL();

            dataGridView_GetUserList.DataSource = usersBLL.GetUserList(currentrPage, perPage, userName);

            int total =  Convert.ToInt32(usersBLL.GetUserCount(userName));
            decimal totalpageDecimal = Convert.ToDecimal( total) / perPage;
            totalPage = Convert.ToInt32(Math.Ceiling(totalpageDecimal));
            if(totalPage == currentrPage || totalPage < 1)
            {
                button_next.Enabled = false;
                button_end.Enabled = false;
            }
            else
            {
                button_next.Enabled = true;
                button_end.Enabled = true;
            }
            label_page.Text = "共有记录 " + total.ToString() + " 页码 " + currentrPage + " / "+ totalPage;
            GlobalData.TotalPage = totalPage;
            comboBox_pageNumber.Items.Clear();
            if (totalPage < 2)
            {
                comboBox_pageNumber.Visible = false;
            }
            else
            {
                comboBox_pageNumber.Visible = true;
                for(int p = 1; p <= totalPage; p++)
                {
                    comboBox_pageNumber.Items.Add(p.ToString());
                }
               comboBox_pageNumber.SelectedItem = currentrPage.ToString();

            }
            
        }

        private void button_index_Click(object sender, EventArgs e)
        {
            GlobalData.CurrentPage = 1;
            GetUserList();
        }

        private void button_end_Click(object sender, EventArgs e)
        {
            GlobalData.CurrentPage = GlobalData.TotalPage;
            GetUserList();
        }

        private void button_next_Click(object sender, EventArgs e)
        {
            GlobalData.CurrentPage = GlobalData.CurrentPage + 1;
            GetUserList();
        }

        private void button_preview_Click(object sender, EventArgs e)
        {
            GlobalData.CurrentPage = GlobalData.CurrentPage - 1;
            GetUserList();
        }

        private void button_go_Click(object sender, EventArgs e)
        {
            string page = comboBox_pageNumber.Text;
            int newPage = Convert.ToInt32(page);
            if(newPage != GlobalData.CurrentPage)
            {
                GlobalData.CurrentPage = newPage;
                GetUserList();
            }
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_search_Click(object sender, EventArgs e)
        {
            string searchContent = textBox_search.Text.Trim();
            if (string.IsNullOrEmpty(searchContent))
            {
                MessageBox.Show("请输入要搜索的内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            GlobalData.CurrentPage = 1;

            button_index.Enabled = false;
            button_preview.Enabled = false;
            GetUserList();
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_UserAdd_Click(object sender, EventArgs e)
        {
            FormUserAdd formUserAdd = new FormUserAdd();
            formUserAdd.TopLevel = true;
            // formUserAdd.Dock = DockStyle.Fill;
            formUserAdd.Activate();
            formUserAdd.Show();
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_UserModify_Click(object sender, EventArgs e)
        {
            // 获取表格当前选择的行,即使点击是单元格，也可以获取整个行
            int index = dataGridView_GetUserList.CurrentRow.Index;
            if(index < 0)
            {
                MessageBox.Show("请选择要修改的用户","提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }

            string userIdStr = dataGridView_GetUserList.Rows[index].Cells[0].Value.ToString();
            int userId = Convert.ToInt32(userIdStr);
            UsersBLL userBLL = new UsersBLL();
            UsersModel user = userBLL.GetUserInfoById(userId);
            usersModel = user;

            // 实例化在后
            // FormUserModify formUserModify = new FormUserModify();
            FormUserModify formUserModify = FormUserModify.CreateInstance();

            // formUserModify.TopLevel = true;
            // formUserModify.Dock = DockStyle.Fill;
            formUserModify.Activate();
            formUserModify.Show();
        }
        /// <summary>
        /// 修改用户权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_UpdatePower_Click(object sender, EventArgs e)
        {
            // 获取表格当前选择的行,即使点击是单元格，也可以获取整个行
            int index = dataGridView_GetUserList.CurrentRow.Index;
            if (index < 0)
            {
                MessageBox.Show("请选择要修改的用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string userIdStr = dataGridView_GetUserList.Rows[index].Cells[0].Value.ToString();
            int userId = Convert.ToInt32(userIdStr);
            UsersBLL userBLL = new UsersBLL();
            UsersModel user = userBLL.GetUserInfoById(userId);
            usersModel = user;
            FormUserPower formUserPower = FormUserPower.CreateInstance();
            formUserPower.Activate();
            formUserPower.Show();
        }
        /// <summary>
        /// 修改用户角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_RoleUpdate_Click(object sender, EventArgs e)
        {
            // 获取表格当前选择的行,即使点击是单元格，也可以获取整个行
            int index = dataGridView_GetUserList.CurrentRow.Index;
            if (index < 0)
            {
                MessageBox.Show("请选择要修改的用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string userIdStr = dataGridView_GetUserList.Rows[index].Cells[0].Value.ToString();
            int userId = Convert.ToInt32(userIdStr);
            UsersBLL userBLL = new UsersBLL();
            UsersModel user = userBLL.GetUserInfoById(userId);
            usersModel = user;
            FormUserRole formUserRole = FormUserRole.CreateInstance();
            formUserRole.Activate();
            formUserRole.Show();
        }
    }
}
